﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class RentWhDetailDA : IRentWhDetailDA
    {
        private string AREA = "@AREA";
        private string CONTRACT_ID = "@CONTRACT_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string LINE_ID = "@LINE_ID";
        private string LOCATION = "@LOCATION";
        private string MANAGE_AMOUNT = "@MANAGE_AMOUNT";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_RENT_WH_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@RENT_WH_DETAIL WHERE CONTRACT_ID = @CONTRACT_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_CONTRACT = " DELETE FROM @_@RENT_WH_DETAIL WHERE CONTRACT_ID = @CONTRACT_ID ";
        private string SQL_DELETE_RENT_WH_DETAIL = " DELETE FROM @_@RENT_WH_DETAIL WHERE CONTRACT_ID = @CONTRACT_ID AND LINE_ID = @LINE_ID ";
        private string SQL_INSERT_RENT_WH_DETAIL = " INSERT INTO @_@RENT_WH_DETAIL ( CONTRACT_ID, LINE_ID, LOCATION, AREA, UNIT_PRICE, TOTAL_PRICE, REMARK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,ZONE_NO,MANAGE_AMOUNT) VALUES ( @CONTRACT_ID, @LINE_ID, @LOCATION, @AREA, @UNIT_PRICE, @TOTAL_PRICE, @REMARK, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE(),@ZONE_NO,@MANAGE_AMOUNT)  ";
        private string SQL_SELECT_ALL_RENT_WH_DETAIL = " SELECT  ROWID, CONTRACT_ID, LINE_ID, LOCATION, AREA, UNIT_PRICE, TOTAL_PRICE, REMARK, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,ZONE_NO,MANAGE_AMOUNT  FROM  @_@RENT_WH_DETAIL ";
        private string SQL_UPDATE_RENT_WH_DETAIL = " UPDATE @_@RENT_WH_DETAIL SET LOCATION = @LOCATION, AREA = @AREA, UNIT_PRICE = @UNIT_PRICE, TOTAL_PRICE = @TOTAL_PRICE, REMARK = @REMARK, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE(),ZONE_NO = @ZONE_NO,MANAGE_AMOUNT = @MANAGE_AMOUNT WHERE CONTRACT_ID = @CONTRACT_ID AND LINE_ID = @LINE_ID  ";
        private string TOTAL_PRICE = "@TOTAL_PRICE";
        private string TS = "@TS";
        private string UNIT_PRICE = "@UNIT_PRICE";
        private string UPDATED_BY = "@UPDATED_BY";
        private string ZONE_NO = "@ZONE_NO";

        public int DeleteRentWhDetail(string contractID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < lineID.Count; i++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CONTRACT_ID", contractID), new SqlParameter("@LINE_ID", lineID[i]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_RENT_WH_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteRentWhDetail(DataBase dataBase, DbTransaction tran, string contractID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < lineID.Count; i++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CONTRACT_ID", contractID), new SqlParameter("@LINE_ID", lineID[i]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_RENT_WH_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteRentWhDetailByID(DataBase dataBase, DbTransaction tran, string contractID, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CONTRACT_ID", contractID) };
                result = dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CONTRACT.Replace("@_@", whLoginID), paras);
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllRentWhDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RENT_WH_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllRentWhDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RENT_WH_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public List<RentWhDetailInfo> GetRentWhDetailByID(string contractID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RENT_WH_DETAIL.Replace("@_@", whLoginID) + " WHERE CONTRACT_ID = @CONTRACT_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CONTRACT_ID", contractID) };
            List<RentWhDetailInfo> rentWhDetailList = new List<RentWhDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    RentWhDetailInfo rentWhDetailInfo = new RentWhDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["CONTRACT_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["LOCATION"].ToString(), (reader["AREA"].ToString() != "") ? decimal.Parse(reader["AREA"].ToString()) : 0M, (reader["UNIT_PRICE"].ToString() != "") ? decimal.Parse(reader["UNIT_PRICE"].ToString()) : 0M, (reader["TOTAL_PRICE"].ToString() != "") ? decimal.Parse(reader["TOTAL_PRICE"].ToString()) : 0M, reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["ZONE_NO"].ToString(), (reader["MANAGE_AMOUNT"].ToString() != "") ? decimal.Parse(reader["MANAGE_AMOUNT"].ToString()) : 0M);
                    rentWhDetailList.Add(rentWhDetailInfo);
                }
            }
            return rentWhDetailList;
        }

        public RentWhDetailInfo GetRentWhDetailByID(string contractID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RENT_WH_DETAIL.Replace("@_@", whLoginID) + " WHERE CONTRACT_ID = @CONTRACT_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CONTRACT_ID", contractID), new SqlParameter("@LINE_ID", lineID) };
            RentWhDetailInfo rentWhDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    rentWhDetailInfo = new RentWhDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["CONTRACT_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["LOCATION"].ToString(), (reader["AREA"].ToString() != "") ? decimal.Parse(reader["AREA"].ToString()) : 0M, (reader["UNIT_PRICE"].ToString() != "") ? decimal.Parse(reader["UNIT_PRICE"].ToString()) : 0M, (reader["TOTAL_PRICE"].ToString() != "") ? decimal.Parse(reader["TOTAL_PRICE"].ToString()) : 0M, reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["ZONE_NO"].ToString(), (reader["MANAGE_AMOUNT"].ToString() != "") ? decimal.Parse(reader["MANAGE_AMOUNT"].ToString()) : 0M);
                }
            }
            return rentWhDetailInfo;
        }

        public RentWhDetailInfo GetRentWhDetailByID(DataBase dataBase, DbTransaction tran, string contractID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RENT_WH_DETAIL.Replace("@_@", whLoginID) + " WHERE CONTRACT_ID = @CONTRACT_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CONTRACT_ID", contractID), new SqlParameter("@LINE_ID", lineID) };
            RentWhDetailInfo rentWhDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                rentWhDetailInfo = new RentWhDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["CONTRACT_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["LOCATION"].ToString(), (reader["AREA"].ToString() != "") ? decimal.Parse(reader["AREA"].ToString()) : 0M, (reader["UNIT_PRICE"].ToString() != "") ? decimal.Parse(reader["UNIT_PRICE"].ToString()) : 0M, (reader["TOTAL_PRICE"].ToString() != "") ? decimal.Parse(reader["TOTAL_PRICE"].ToString()) : 0M, reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["ZONE_NO"].ToString(), (reader["MANAGE_AMOUNT"].ToString() != "") ? decimal.Parse(reader["MANAGE_AMOUNT"].ToString()) : 0M);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return rentWhDetailInfo;
        }

        public DataSet GetRentWhDetailByQueryList(List<string> sqlWhere, RentWhDetailQueryEntity rentwhdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_RENT_WH_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!rentwhdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, rentwhdetailQuery.CurrentPage, rentwhdetailQuery.PageSize, rentwhdetailQuery.SortField, rentwhdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetRentWhDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, RentWhDetailQueryEntity rentwhdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_RENT_WH_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, rentwhdetailQuery.CurrentPage, rentwhdetailQuery.PageSize, rentwhdetailQuery.SortField, rentwhdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertRentWhDetail(RentWhDetailInfo rentWhDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_RentWhDetail_Parameters(rentWhDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_RENT_WH_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertRentWhDetail(DataBase dataBase, DbTransaction tran, RentWhDetailInfo rentWhDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_RentWhDetail_Parameters(rentWhDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_RENT_WH_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_RentWhDetail_Parameters(RentWhDetailInfo rentWhDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.CONTRACT_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.LOCATION, SqlDbType.VarChar, 50), new SqlParameter(this.AREA, SqlDbType.Decimal, 9), new SqlParameter(this.UNIT_PRICE, SqlDbType.Decimal, 9), new SqlParameter(this.TOTAL_PRICE, SqlDbType.Decimal, 9), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.ZONE_NO, SqlDbType.VarChar, 50), new SqlParameter(this.MANAGE_AMOUNT, SqlDbType.Decimal, 9) };
            paramArray[0].Value = rentWhDetailInfo.ContractID;
            paramArray[1].Value = rentWhDetailInfo.LineID;
            paramArray[2].Value = rentWhDetailInfo.Location;
            paramArray[3].Value = rentWhDetailInfo.Area;
            paramArray[4].Value = rentWhDetailInfo.UnitPrice;
            paramArray[5].Value = rentWhDetailInfo.TotalPrice;
            if (!string.IsNullOrEmpty(rentWhDetailInfo.Remark))
            {
                paramArray[6].Value = rentWhDetailInfo.Remark;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            paramArray[7].Value = rentWhDetailInfo.CreatedBy;
            paramArray[8].Value = rentWhDetailInfo.UpdatedBy;
            if (!string.IsNullOrEmpty(rentWhDetailInfo.ZoneNO))
            {
                paramArray[9].Value = rentWhDetailInfo.ZoneNO;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(rentWhDetailInfo.ManageAmount.ToString()))
            {
                paramArray[10].Value = rentWhDetailInfo.ManageAmount;
                return paramArray;
            }
            paramArray[10].Value = 0;
            return paramArray;
        }

        public int UpdateRentWhDetail(RentWhDetailInfo rentWhDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_RentWhDetail_Parameters(rentWhDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_RENT_WH_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateRentWhDetail(DataBase dataBase, DbTransaction tran, RentWhDetailInfo rentWhDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_RentWhDetail_Parameters(rentWhDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_RENT_WH_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

